<?php if(isset($this->data['showBrands'])):?>
<div id="result">
<form method="post" id="import_global_brand" action="<?php echo Configure::read('base_url')."add_category/editcategory";?>">
<?php echo $this->Form->input('Category.id', array( 'type' => 'hidden', 'value' => $this->data['id'] ));?>

<table width="100%" border="0" cellspacing="0" cellpadding="0" id="mytable" >
<?php if(isset($this->data['global_brand'])):?>
  <tr><td colspan="2"><h5>Import Global Brands</h5></td></tr>
  <tr>
    <td width="30"><input type="checkbox" id="select_brand_all"></td>
    <td width="97%">Select all</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td><table width="100%" border="0" cellspacing="0" cellpadding="0" class="import_global_brand">
	<?php $i=0;foreach($this->data['global_brand'] as $brand):
		$class = null;
		if ($i++ % 2 == 0)
		$class = ' class="alt"';
		
	?>
      <tr>
        <td width="30" <?php echo $class;?>>
		<?php $checked = '';	
		if(isset($this->data['selected_brand'])){
		foreach($this->data['selected_brand'] as $global):					
			if($global['Categorybrand']['brand_id'] == $brand['Brand']['id']){
				$checked = ' checked="checked"';
				echo'<input type="hidden" name="data[updateid]['.$brand['Brand']['id'].']" value="'.$global['Categorybrand']['id'].'" />';
			}
			endforeach;		
		}	
		?>
		<input type="checkbox"  onclick="return chkforproduct(this);" <?php echo $checked;?> name="data[global_brand][]" id="global_brand" value="<?php echo $brand['Brand']['id'];?>" /></td>
        <td <?php echo $class;?>><?php echo $brand['Brand']['brand_description'];?></td>
      </tr>
    <?php endforeach;?>  
    </table></td>
  </tr>
  <?php endif;?>
  
  <?php if(isset($this->data['global_style'])):?>
  <tr><td colspan="2"><h5>Import Global Style</h5></td></tr>
  <tr>
    <td width="30"><input type="checkbox" id="select_style_all"></td>
    <td width="97%">Select all</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td><table width="100%" border="0" cellspacing="0" cellpadding="0"  class="import_global_style">
	<?php $i=0;foreach($this->data['global_style'] as $brand):
		$class = null;
		if ($i++ % 2 == 0) 
		$class = ' class="alt"';
	?>
      <tr>
        <td width="30" <?php echo $class;?>>
		<?php $checked = '';	
		if(isset($this->data['selected_style'])){
		foreach($this->data['selected_style'] as $global):					
			if($global['Categorybrand']['brand_id'] == $brand['Brand']['id']){
				$checked = ' checked="checked"';			
				echo'<input type="hidden" name="data[updateid]['.$brand['Brand']['id'].']" value="'.$global['Categorybrand']['id'].'" />';
			}	
			endforeach;		
		}	
		?>
		<input type="checkbox" onclick="return chkforproduct(this);"  <?php echo $checked;?> name="data[global_style][]" id="global_style" value="<?php echo $brand['Brand']['id'];?>" /></td>
        <td <?php echo $class;?>><?php echo $brand['Brand']['brand_description'];?></td>
      </tr>
    <?php endforeach;?>  
    </table></td>
  </tr>
  <?php endif;?>
  
  <?php if(isset($this->data['global_condition'])):?>
  <tr><td colspan="2"><h5>Import Global Condition</h5></td></tr>
  <tr>
    <td width="30"><input type="checkbox" id="select_condition_all"></td>
    <td width="97%">Select all</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td><table width="100%" border="0" cellspacing="0" cellpadding="0"  class="import_global_condition">
	<?php $i=0;foreach($this->data['global_condition'] as $brand):
		$class = null;
		if ($i++ % 2 == 0)
		$class = ' class="alt"';
	?>
      <tr>
        <td width="30" <?php echo $class;?>>
		<?php $checked = '';	
		if(isset($this->data['selected_condition'])){
		foreach($this->data['selected_condition'] as $global):
			if($global['Categorybrand']['brand_id'] == $brand['Brand']['id']){
				$checked = ' checked="checked"';
				echo'<input type="hidden" name="data[updateid]['.$brand['Brand']['id'].']" value="'.$global['Categorybrand']['id'].'" />';
			}
			endforeach;
		}	
		?>
		<input type="checkbox"  onclick="return chkforproduct(this);" <?php echo $checked;?> name="data[global_condition][]" id="global_condition" value="<?php echo $brand['Brand']['id'];?>" /></td>
        <td <?php echo $class;?>><?php echo $brand['Brand']['brand_description'];?></td>
      </tr>
    <?php endforeach;?>  
    </table></td>
  </tr>
  <?php endif;?>
  <tr>
    <td colspan="2"><table width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td width="75"><input type="submit" name="button" id="button" value="Import" /></td>
        <td><input type="submit" name="button2" id="button2" value="Skip This!" /></td>
      </tr>
    </table></td>
    </tr>
</table>

</form>
</div>
<?php  echo $html->script(array("jquery.min.js","jquery.form.js","jquery.validate.js","jquery.pagination.js","jquery.tablesorter.min.js","jquery.tablednd.js")); ?>	 
 <script type="text/javascript">  
 jQuery(document).ready(function(){
 jQuery("#category_table").tablesorter({headers: { 2:{sorter: false},3:{sorter: false}, 4:{sorter: false}, 5:{sorter: false}}}); 
 		
		var v = jQuery("#import_global_brand").validate({
			submitHandler: function(form) {
				jQuery(form).ajaxSubmit({
					target: "#result"
				});
			}
		});		
		
//Check all
		jQuery("#select_brand_all").click(function()				
			{
				var checked_status = this.checked;
				if(checked_status==false){
					if(!confirm("Are you sure you want to remove all brands form the category?")){
						return false;
					}					
				}
				
				jQuery("input[id=global_brand]").each(function()
				{
					this.checked = checked_status;
				});
			});
			
			jQuery("#select_style_all").click(function()				
			{
				var checked_status = this.checked;
				if(checked_status==false){
					if(!confirm("Are you sure you want to remove all styles form the category?")){
						return false;
					}					
				}
				jQuery("input[id=global_style]").each(function()
				{
					this.checked = checked_status;
				});
			});
			jQuery("#select_condition_all").click(function()				
			{
				var checked_status = this.checked;
				if(checked_status==false){
					if(!confirm("Are you sure you want to remove all conditions form the category?")){
						return false;
					}					
				}
				jQuery("input[id=global_condition]").each(function()
				{
					this.checked = checked_status;
				});
			});
	});
	
	function chkforproduct(param){
			if(param.checked==false)
			{
				catID=jQuery("#CategoryId").val();
				brandID=jQuery(param).val();
				var brandType="";
				if(jQuery(param).attr('id')=='global_style')
				brandType='style';
				else if(jQuery(param).attr('id')=='global_condition')
				brandType='condition';
				else
				brandType='brand';
				data="catid="+catID+"&brandid="+brandID;
				jQuery.ajax({
					type: 'POST',
					url: baseurl+"add_category/chkforproduct/"+brandType,
					data: data,
					dataType: 'json',
					success: function(data){						
						if(data.message=="Yes"){
							if(confirm("There are "+data.total+" Products Under this brands. Are you sure you want to remove form the category?")){
								
								
							}
							else
							{
								jQuery(param).attr('checked','checked');								
							}
						}else{
						
						
						}
					}			
				});
			}
		}
var brands=jQuery(".import_global_brand");
var styles=jQuery(".import_global_style");
var condition=jQuery(".import_global_condition");
var brandsCount=1;
var styleCount=1;
var condtionCount=1;


function test(selector){
    selector.find("tr").show();
    className=selector.attr("class");
    if(className=="import_global_brand"){
        count=brandsCount;
        brandsCount++;
    }
    else if(className=="import_global_style"){
        count=styleCount;
        styleCount++;
    }
    else{
        count=condtionCount;
        condtionCount++;
    }
    selector.find("tr").each(function(index){
        if(index > count*5 && index < selector.find("tr").length-1){
            jQuery(this).hide();
        }

    });
    
    menuHide(selector);

}

function lessItem(selector){
    className=selector.attr("class");
    if(className=="import_global_brand"){
        count=brandsCount;
        brandsCount=brandsCount-2;
    }
    else if(className=="import_global_style"){
        count=styleCount;
        styleCount=styleCount-2;
    }
    else{
        count=condtionCount;
        condtionCount=condtionCount-2;
    }

    test(selector);
    menuHide(selector);
}

function menuHide(selector){
    length=parseInt(selector.find("tr").length);
    hiddenFields=parseInt(selector.find("tr:hidden").length);
    
    diff=parseInt(length)-hiddenFields; 
    if(diff <= 7){
        selector.find(".less").hide();
        selector.find(".more").show();
        selector.find(".pipe").hide();
    }

    if(hiddenFields==0){
        selector.find(".less").show();
        selector.find(".more").hide();
        selector.find(".pipe").hide();
    }

    if(diff > 7 && hiddenFields > 0){
        selector.find(".less").show();
        selector.find(".more").show();
        selector.find(".pipe").show();
    }
}

jQuery(".import_global_brand").append('<tr><td></td><td style="text-align: right;"><a onclick="lessItem(brands)" class="less"><< Less</a><span class="pipe"> || </span> <a onclick="test(brands)" class="more">More >></a></td></tr>');
jQuery(".import_global_style").append('<tr><td></td><td style="text-align: right;"><a onclick="lessItem(styles)" class="less"><< Less</a><span class="pipe"> || </span> <a onclick="test(styles)" class="more">More >></a></td></tr>');
jQuery(".import_global_condition").append('<tr><td></td><td style="text-align: right;"><a onclick="lessItem(condition)" class="less"><< Less</a><span class="pipe"> || </span> <a onclick="test(condition)" class="more">More >></a></td></tr>');

test(brands);
test(styles);
test(condition);
</script>
<style type="text/css">
.more,.less{cursor: pointer;}
</style>		
 
<?php else:?>
<div id="result">
<h5>Edit <?php echo $this->data['Category']['category_name'];?></h5>
<form method="post" id="add_category" action="<?php echo Configure::read('base_url')."add_category/edit";?>">
<table cellspacing="0" id="mytable">
          <tbody>          
		   <?php foreach($this->data['brand_settings'] as $brand_settings):?>
		  <?php if($brand_settings['Brandsetting']['brand_type']=="brand" && $brand_settings['Brandsetting']['prompt']=="1"):?>
          <tr>
            <td width="27" class="alt"><input type="checkbox"  value="Yes" name="data[global][brand]" checked="checked"  id="checkbox4" /></td>
            <td width="462" class="alt">Show me a list of Global brands to assign them on this category</td>
            </tr>
		<?php elseif($brand_settings['Brandsetting']['brand_type']=="style" && $brand_settings['Brandsetting']['prompt']=="1"):?>			
          <tr>
            <td><input type="checkbox" value="Yes" name="data[global][style]"  checked="checked"  id="checkbox5" /></td>
            <td>Show me a list of Global styles to assign them on this category</td>
            </tr>
		<?php elseif($brand_settings['Brandsetting']['brand_type']=="condition" && $brand_settings['Brandsetting']['prompt']=="1"):?>				
          <tr>
            <td class="alt"><input type="checkbox" value="Yes" name="data[global][condition]" checked="checked"  id="checkbox6" /></td>
            <td class="alt">Show me a list of Global conditions to assign them on this category</td>
            </tr>
		<?php endif; endforeach;?>		
          <tr>
            <td colspan="2"><table width="100%" border="0" cellspacing="0" cellpadding="0">
              <tr>
                <td width="144">
					<input type="hidden" name="data[id]" value="<?php echo $this->data['Category']['id'];?>" />
					
					<?php echo $this->Form->input('category_name',array('label'=>false,'size'=>30,'class'=>"required",'value'=>$this->data['Category']['category_name'], 'title'=>"Enter category name here")); ?>
				</td>
                <td width="347"><input type="submit" style="background:#73AE0B; color:#fff; font-size:14px;" name="button" id="button" value="Submit" /></td>
              </tr>
            </table></td>
            </tr>
        </tbody></table>
</form>
</div>
<?php  echo $html->script(array("jquery.min.js","jquery.form.js","jquery.validate.js","jquery.pagination.js","jquery.tablesorter.min.js","jquery.tablednd.js")); ?>	 
 <script type="text/javascript">  
 jQuery(document).ready(function(){
 jQuery("#category_table").tablesorter({headers: { 2:{sorter: false},3:{sorter: false}, 4:{sorter: false}, 5:{sorter: false}}}); 
 		
// show a simple loading indicator
		var loader = jQuery('<div id="loader"><img src="<?php echo Configure::read('base_url');?>img/loading.gif" alt="loading..." /></div>')
			.css({position: "relative", top: "1em", left: "25em"})
			.appendTo("body")
			.hide();
		jQuery().ajaxStart(function() {
			loader.show();
		}).ajaxStop(function() {
			loader.hide();
		}).ajaxError(function(a, b, e) {
			throw e;
		});
		
		var v = jQuery("#add_category").validate({
			submitHandler: function(form) {
				jQuery(form).ajaxSubmit({
					target: "#result"
				});
			}
		});		
		
	});

 <?php endif;?>